package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

import java.util.LinkedHashMap;

public class PuzzleGameDialogActivity extends Activity {
    private HttpConfig mHttpConfig;
    private ImageView puzzledialog_iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_puzzlegamedialog);
        mHttpConfig = new HttpConfig(PuzzleGameDialogActivity.this);
        initView();
        initData();
    }

    private void initView() {
        findViewById(R.id.puzzledialog_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        puzzledialog_iv = (ImageView) findViewById(R.id.puzzledialog_iv);
        puzzledialog_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(PuzzleGameDialogActivity.this, PuzzleSelectActivity.class));
                finish();
            }
        });

    }


    private void initData() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        StringRequest req = new StringRequest(getString(R.string.ynGetActivityBanner))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (ReqUtils.isSuccess(json.optString("code"))) {
                                String pic = json.optJSONArray("data").optJSONObject(0).optString("bpic");
                                ImageLoader.getInstance().displayImage(pic, puzzledialog_iv);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Toast.makeText(PuzzleGameDialogActivity.this, CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        LiteHttp.newApacheHttpClient(new HttpConfig(this)).executeAsync(req);
    }
}
